package com.ziyia.a8_2_2;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.R)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        boolean externalStorageManager = Environment.isExternalStorageManager();


        if (!externalStorageManager) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 20);
        }
        Toast.makeText(this, "" + externalStorageManager, Toast.LENGTH_SHORT).show();
//

    }

    public void read(View view) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "SD卡不可用~", Toast.LENGTH_SHORT).show();
        }

        File dir = getExternalFilesDir(null);
        do {
            dir = Objects.requireNonNull(dir).getParentFile();
        } while (Objects.requireNonNull(dir).getAbsolutePath().contains("/Android"));

//
        File file = new File(dir, "xls.txt");


//        String file = "/sdcard/xls.txt";
//        Toast.makeText(this, "文件是否存在 = " + file.exists(), Toast.LENGTH_SHORT).show();
        try {
//            FileInputStream fileInput = openFileInput(file.toString());
            FileInputStream fileInput = new FileInputStream(file);

            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInput));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                Log.d("-----------", line);
            }
            new AlertDialog.Builder(this)
                    .setTitle(getLocalClassName())
                    .setMessage(builder)
                    .create()
                    .show();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            }
        }
    }


}